<?php

namespace Admin\Controller;

class ClassController extends AdminController {

    private $token;
    private $deviceid;
    private $user_id;
    private $class;
    private $userinfo;

    function __construct() {
        parent::__construct();
        $this->error_class = L('error_class');
        $this->token = I('request.token');
        $this->deviceid = I('request.deviceid');
        $this->userinfo = $this->verify_token($this->token, $this->deviceid);
        $this->user_id = $this->userinfo['user_id'];
    }

    private function is_power($user_id, $class_id, $power = 10) {
        $where = array('user_id' => $user_id, 'class_id' => $class_id);
        $classmember = M('Classmember')->where($where)->limit(1)->select('power');
        if ($classmember['power'] >= $power) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //班级code加密函数
    private function getclasscode() {
        $char = getsalt(1);
        return substr(strtoupper(strrev(dechex(time()))), 0, 7) . $char;
    }

    public function createclass() {
        $classname = I('request.classname');
        $description = I('request.description');
        if (!$this->is_teacher($this->user_id)) {
            $this->error($this->error_class['noteacher']);
        }
        if (strlen($classname) < 8 || strlen($classname) > 20) {
            $this->error($this->error_class['longername']);
        }
        $class_code = $this->getclasscode();
        $Class = D('Class');
        $class_id = $Class->addClass($classname, $description, $this->user_id, $username, $class_code);
        if ($class_id) {
            $ClassMember = D('Member');
            $ClassMember->addMember($this->user_id, $class_id, $username, 10);
            $arrInfo = array(
                'classname' => $classname,
                'description' => $description,
                'class_id' => $class_id,
                'code' => $class_code,
            );
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    public function upclass() {
        $class_id = I('request.classid');
        $classname = I('request.classname');
        $description = I('request.description');
        if (!$this->is_power($this->user_id, $class_id)) {
            $this->error($this->error_class['noadmin']);
        }
        $updata = array(
            'classname' => $classname,
            'description' => $description
        );
        $Class = D('Class');
        if ($Class->upClass($class_id, $updata)) {
            $updata['class_id'] = $class_id;
            $arrInfo = $updata;
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    public function upclasscode() {
        $class_id = I('request.classid');
        if (!$this->is_power($this->user_id, $class_id)) {
            $this->error($this->error_class['noadmin']);
        }
        $class_code = $this->getclasscode();
        $updata = array(
            'code' => $class_code
        );
        $Class = D('Class');
        if ($Class->upClass($class_id, $updata)) {
            $arrInfo = $updata;
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

    public function delclass() {
        echo __FUNCTION__;
    }

    public function getclassinfo() {
        $class_id = I('request.classid');
        $Member = D('Member');
        $meminfo = $Member->getMembyids($this->user_id, $class_id);
        if (!$meminfo['power'] || empty($meminfo['power'])) {
            $this->error($this->error_class['noinclass']);
        }
        $Class = D('Class');
        $Post = D('Post');
        $info = $Class->getClassbycid($class_id);
        $info['number'] = $Member->getCountbycid($class_id);
        $info['postnum'] = $Post->getPostnumbycid($class_id);
        $this->success(__FUNCTION__, $info);
    }

    public function getclassmembers() {
        $limit = 30;
        $class_id = I('request.classid');
        $start = I('request.startid') != FALSE ? I('request.startid') : 0;
        $Member = D('Member');
        $meminfo = $Member->getMembyids($this->user_id, $class_id);
        if (!$meminfo['power'] || empty($meminfo['power'])) {
            $this->error($this->error_class['noinclass']);
        }
        $info['members'] = $Member->getList($class_id, $start,$limit);
        $info['page'] = $start * $limit + $limit;
        $this->success(__FUNCTION__, $info);
    }

    public function upmemberpower() {
        $power = I('I.power');
        $user_id = I('I.userid');
        $class_id = I('I.classid');
        if (!$this->is_power($this->user_id, $class_id)) {
            $this->error($this->error_class['noadmin']);
        }
        $Member = D('Member');
        $member = $Member->getMembyids($user_id, $class_id);
        if (!$member['id'] || empty($member['id'])) {
            $this->error($this->error_class['oserror']);
        }
        if ($member['power'] == $power) {
            $this->error($this->error_class['ispower']);
        }
        $updata = array('power' => $power);
        if ($Member->upMemberbyids($user_id, $class_id, $updata)) {
            $info = $member;
            $member['power'] = $power;
            $this->success(__FUNCTION__, $info);
        } else {
            $this->error($this->error_class['oserror']);
        }
    }

}
